package com.ruoyi.quartz.task;


import com.ruoyi.quartz.domain.LotTicket;
import com.ruoyi.quartz.mapper.LotTicketMapper;
import com.xuxueli.crawler.XxlCrawler;
import com.xuxueli.crawler.parser.PageParser;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Component("ticketTask")
public class TicketTask {

    @Resource
    private LotTicketMapper lotTicketMapper;

    public void ticketHistory(String year,String lotId){

        Map<String ,String> params = new HashMap<>();
        params.put("lotid",lotId);
        params.put("year",year);
        params.put("limit","");

        XxlCrawler crawler = new XxlCrawler.Builder()
                .setWhiteUrlRegexs("https://www.17500.cn/api/kaijiang/getlist")
                .setAllowSpread(false)
                .setIfPost(true)
                .setParamMap(params)
                .setUrls("https://www.17500.cn/api/kaijiang/getlist")
                .setPageParser(new PageParser(){

                    @Override
                    public void parse(Document document, Element element, Object o) {
                        Element tbody = element.getElementsByTag("tbody").get(0);

                        LotTicket lotTicket = null;
                        Elements trs = tbody.children();
                        Elements tds = null;
                        for (Element tr:trs){

                            tds = tr.children();

                            if (tds.size()>0){
                                lotTicket =  new LotTicket();
                                lotTicket.setOpenCode(tds.get(0).text());
                                lotTicket.setOpenDate(tds.get(1).text());
                                lotTicket.setOpenNumber(tds.get(2).text());
                                lotTicket.setOpenOrder(tds.get(3).text());
                                lotTicket.setTicketTypeId(1l);
                                lotTicketMapper.insertLotTicket(lotTicket);
                            }


                        }

                    }
                })
                .build();

        crawler.start(true);

    }


    public void sevenStarTicket (String year,String lotId){

        Map<String ,String> params = new HashMap<>();
        params.put("lotid",lotId);
        params.put("year",year);
        params.put("limit","");

        XxlCrawler crawler = new XxlCrawler.Builder()
                .setUrls("https://www.17500.cn/api/kaijiang/getlist")
                .setWhiteUrlRegexs("https://www.17500.cn/api/kaijiang/getlist")
                .setIfPost(true)
                .setAllowSpread(false)
                .setParamMap(params)
                .setFailRetryCount(3)
                .setPageParser(new PageParser() {
                    @Override
                    public void parse(Document document, Element element, Object o) {
                        Element tbody = element.getElementsByTag("tbody").get(0);

                        LotTicket lotTicket = null;
                        Elements trs = tbody.children();
                        Elements tds = null;

                        for (Element tr:trs){

                            tds = tr.children();

                            if (tds.size()>0){
                                lotTicket =  new LotTicket();
                                lotTicket.setOpenCode(tds.get(0).text());
                                lotTicket.setOpenDate(tds.get(1).text());
                                lotTicket.setOpenNumber(tds.get(2).text());
                                lotTicket.setOpenOrder(tds.get(2).text().substring(0,4));
                                lotTicket.setTicketTypeId(3l);
                                lotTicketMapper.insertLotTicket(lotTicket);
                            }


                        }
                    }
                })
                .build();

        crawler.start(true);
    }

}
